<?php

class MinimalAtomStorage extends WakStorage {

    static function Factory($kit_id,$cfg) {
        return new MinimalAtomStorage($kit_id,$cfg);
    }

    protected $cfg=array(
        'storage'=>array(
            'path'=>'entries/'
            , 'search'=>'{{published}}.html'
            ,'class'=>'AtomEntryBean')
        ,'feed'=>array(
            'id'=>''));

    /**
     * get the latest entries and build the feed
     * @access public
     * @return AtomFeedBean
     */
    function getFeed($qt=20) {
        // listing files
        // {pubdate}{.title}.entry.html
        static $regex='([^\.]+)(\.[^\.]+)?\.html';
        if (!$dir=dir($this->path)) {
            return false;
        }
        while ($path=$dir->read()) {
            // avoiding path starting by .
            if (substr($path,0,1)=='.') continue;
            if (ereg($regex,$path,$found)) {
                $published=$found[1];
                $title=ereg_replace('[-_]',' ',substr($found[2],1));
                $key=$published.$title;
                $files[$key]=array(
                    'published'=>$published
                    , 'path'=>$path
                    ,'title'=>$title);
            }
        }
        // sorting by published desc
        krsort($files);
        // echo '<pre>'; print_r($files);
        // building feed
        $feed=new AtomFeedBean();
        $feed->set('id',$this->cfg['feed']['id']);
        $n=1;
        foreach ($files as $file) {
            $filename=$this->path.$file['path'];
            // building entry
            $entry=new AtomEntryBean();
            //$entry->id=$feed->id.'/'.$n;
            // setting published
            $entry->set('published',$file['published']);
            // setting content from file content
            $content=file_get_contents($filename);
            $entry->set('content',$content);
            // setting updated from file updated time
            $entry->set('updated',filemtime($filename));
            // setting title
            $entry->set('title',$file['title']);
            // echo '<pre>'; print_r($entry);
            $feed->addEntry($entry);
            $n++;
        }
        // print_r($feed);
        return $feed;
    }

    /**
     * builds an entry bean from filename
     */
    protected function _buildEntry($filename) {

    }

    /**
     * list entry files, ordered by published desc
     * @return array
     */
    function _listEntryFiles() {
        static $regex='(.*)\.entry.html';
        if (!$dir=dir($this->path)) {
            return false;
        }
        while ($path=$dir->read()) {
            // avoiding path starting by .
            if (substr($path,0,1)=='.') continue;
            if (ereg($regex,$path,$found)) {
                $published=$found[1];
                $entries[$published]=$path;
            }
        }
        krsort($entries);
        return $entries;
    }

}
